{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spherical Coordinates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>© Von P. Walden, Washington State University</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conversion between cartesian and spherical coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x = R sin(\\theta) cos(\\phi)$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle y = R sin(\\theta) sin(\\phi)$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle z = R cos(\\theta)$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display, Math, Latex\n",
    "display(Math(r'x = R sin(\\theta) cos(\\phi)'))\n",
    "display(Math(r'y = R sin(\\theta) sin(\\phi)'))\n",
    "display(Math(r'z = R cos(\\theta)'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert cartesian point (1,1,1) to spherical coordinates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = 1.\n",
    "y = 1.\n",
    "z = 1.\n",
    "# z = sqrt(2.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R =  1.7320508075688772\n"
     ]
    }
   ],
   "source": [
    "R = (x**2 + y**2 + z**2)**0.5\n",
    "print('R = ', R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "theta =  0.9553166181245092 radians\n",
      "theta =  54.735610317245346 degrees\n"
     ]
    }
   ],
   "source": [
    "theta = arccos(z/R)\n",
    "print('theta = ', theta, 'radians')\n",
    "print('theta = ', theta*180./pi, 'degrees')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phi =  0.7853981633974485 radians\n",
      "phi =  45.00000000000001 degrees\n"
     ]
    }
   ],
   "source": [
    "# Use y to find phi.\n",
    "phi = arcsin(y/(R * sin(theta)))\n",
    "print('phi = ', phi, 'radians')\n",
    "print('phi = ', phi*180./pi, 'degrees')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phi =  0.7853981633974481 radians\n",
      "phi =  44.999999999999986 degrees\n"
     ]
    }
   ],
   "source": [
    "# Use x to find phi.\n",
    "phi = arccos(x/(R * sin(theta)))\n",
    "print('phi = ', phi, 'radians')\n",
    "print('phi = ', phi*180./pi, 'degrees')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phi =  0.7853981633974483 radians\n",
      "phi =  45.0 degrees\n"
     ]
    }
   ],
   "source": [
    "# Use both x and y to find phi.\n",
    "phi = arctan(y/x)\n",
    "print('phi = ', phi, 'radians')\n",
    "print('phi = ', phi*180./pi, 'degrees')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "widgets": {
   "state": {},
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
